<?php
namespace BP\GPM;
require_once("BP/En/Map.php");
class Dept extends EntityTree {
	protected $_HisSubDepts;	// Depts
	private function __init() { // default class members
		$this->_HisSubDepts = NULL;
	}
	public function getNameOfPath () 
	{
		return $this->GetValStrByKey(DeptAttr::$NameOfPath);
	}
	public function setNameOfPath ($value) // [String value]
	{
		$this->SetValByKey(DeptAttr::$NameOfPath, $value);
	}
	public function getParentNo () 
	{
		return $this->GetValStrByKey(DeptAttr::$ParentNo);
	}
	public function setParentNo ($value) // [String value]
	{
		$this->SetValByKey(DeptAttr::$ParentNo, $value);
	}
	public function getHisSubDepts () 
	{
		if (($this->_HisSubDepts == NULL))
		{
			$this->_HisSubDepts = Depts::constructor__String($this->getNo());
		}
		return $this->_HisSubDepts;
	}
	public static function constructor__ () 
	{
		$me = new self();
		$me->__init();
		return $me;
	}
	public static function constructor__String ($no) // [String no]
	{
		$me = new self();
		$me->__init();
		return $me;
	}
	public function getHisUAC () 
	{
		$uac = new UAC();
		$uac->OpenForSysAdmin();
		return $uac;
	}
	public function getEnMap () 
	{
		if (($this->get_enMap() != NULL))
		{
			return $this->get_enMap();
		}
		$map = new Map();
		$map->setEnDBUrl(new DBUrl($DBUrlType->AppCenterDSN));
		$map->setPhysicsTable("Port_Dept");
		$map->Java_SetEnType($EnType->Admin);
		$map->setEnDesc("部门");
		$map->Java_SetDepositaryOfEntity($Depositary->Application);
		$map->Java_SetDepositaryOfMap($Depositary->Application);
		$map->AddTBStringPK(DeptAttr::$No, NULL, "编号",  TRUE ,  TRUE , 1, 50, 20);
		$map->AddTBString(DeptAttr::$Name, NULL, "名称",  TRUE ,  FALSE , 0, 100, 30);
		$map->AddTBString(DeptAttr::$NameOfPath, NULL, "部门路径",  TRUE ,  TRUE , 0, 300, 30,  TRUE );
		$map->AddTBString(DeptAttr::$ParentNo, NULL, "父节点编�?",  TRUE ,  FALSE , 0, 100, 30);
		$map->AddTBInt(DeptAttr::$Idx, 0, "顺序�?",  TRUE ,  FALSE );
		$rm = new RefMethod();
		$rm->Title = "重置该部门一下的部门路径";
		$rm->ClassMethodName = ($this . ".DoResetPathName");
		$rm->refMethodType = $RefMethodType->Func;
		$msg = "当该部门名称变化�?,该部门与该部门的子部门名称路�?(Port_Dept.NameOfPath)将发生变�?.";
		$msg += "\t\n 该部门与该部门的子部门的人员路径也要发生变化Port_Emp列DeptDesc.StaDesc.";
		$msg += "\t\n 您确定要执行�??";
		$rm->Warning = $msg;
		$map->AddRefMethod($rm);
		$rm = new RefMethod();
		$rm->Title = "增加同级部门";
		$rm->ClassMethodName = ($this . ".DoSameLevelDept");
		$rm->getHisAttrs()->AddTBString("No", NULL, "同级部门编号",  TRUE ,  FALSE , 0, 100, 100);
		$rm->getHisAttrs()->AddTBString("Name", NULL, "部门名称",  TRUE ,  FALSE , 0, 100, 100);
		$map->AddRefMethod($rm);
		$rm = new RefMethod();
		$rm->Title = "增加下级部门";
		$rm->ClassMethodName = ($this . ".DoSubDept");
		$rm->getHisAttrs()->AddTBString("No", NULL, "同级部门编号",  TRUE ,  FALSE , 0, 100, 100);
		$rm->getHisAttrs()->AddTBString("Name", NULL, "部门名称",  TRUE ,  FALSE , 0, 100, 100);
		$map->AddRefMethod($rm);
		$map->getAttrsOfOneVSM()->AddBranchesAndLeaf(DeptEmps::constructor__(), Emps::constructor__(), DeptEmpAttr::$FK_Dept, DeptEmpAttr::$FK_Emp, "对应人员", EmpAttr::$FK_Dept, EmpAttr::$Name, EmpAttr::$No, "@WebUser.getFK_Dept()");
		$map->getAttrsOfOneVSM()->AddGroupPanelModel(DeptStations::constructor__(), Stations::constructor__(), DeptStationAttr::$FK_Dept, DeptStationAttr::$FK_Station, "对应岗位(平铺)", StationAttr::$FK_StationType);
		$map->getAttrsOfOneVSM()->AddGroupListModel(DeptStations::constructor__(), Stations::constructor__(), DeptStationAttr::$FK_Dept, DeptStationAttr::$FK_Station, "对应岗位(�?)", StationAttr::$FK_StationType);
		$this->set_enMap($map);
		return $this->get_enMap();
	}
	public function DoMyCreateSubNode () 
	{
		$en = $this->DoCreateSubNode();
		return $en->ToJson();
	}
	public function DoMyCreateSameLevelNode () 
	{
		$en = $this->DoCreateSameLevelNode();
		return $en->ToJson();
	}
	public function DoSameLevelDept ($no, $name) // [String no, String name]
	{
		$en = Dept::constructor__();
		$en->setNo($no);
		if (($en->RetrieveFromDBSources() == 1))
		{
			return "err@编号已经存在";
		}
		$en->setName($name);
		$en->setParentNo($this->getParentNo());
		$en->Insert();
		return "增加成功..";
	}
	public function DoSubDept ($no, $name) // [String no, String name]
	{
		$en = Dept::constructor__();
		$en->setNo($no);
		if (($en->RetrieveFromDBSources() == 1))
		{
			return "err@编号已经存在";
		}
		$en->setName($name);
		$en->setParentNo($this->getNo());
		$en->Insert();
		return "增加成功..";
	}
	public function DoResetPathName () 
	{
		$this->GenerNameOfPath();
		return "重置成功.";
	}
	public function GenerNameOfPath () 
	{
		$name = $this->getName();
		if (($this->getIsRoot() ==  TRUE ))
		{
			$this->setNameOfPath($name);
			$this->DirectUpdate();
			$this->GenerChildNameOfPath($this->getNo());
			return ;
		}
		$dept = Dept::constructor__();
		$dept->setNo($this->getParentNo());
		if (($dept->RetrieveFromDBSources() == 0))
		{
			return ;
		}
		while ( TRUE ) 
		{
			if ($dept->getIsRoot())
			{
				break;
			}
			$name = (($dept->getName() . "\\") . $name);
			$dept = Dept::constructor__String($dept->getParentNo());
		}
		$name = (($dept->getName() . "\\") . $name);
		$this->setNameOfPath($name);
		$this->DirectUpdate();
		$this->GenerChildNameOfPath($this->getNo());
		$emps = Emps::constructor__();
		$emps->Retrieve(EmpAttr::$FK_Dept, $this->getNo());
		foreach ($emps->ToJavaList() as $emp) 		{
			$emp->Update();
		}
	}
	public function GenerChildNameOfPath ($deptNo) // [String deptNo]
	{
		$depts = Depts::constructor__String($deptNo);
		if ((($depts != NULL) && ($depts->size() > 0)))
		{
			foreach ($depts->ToJavaList() as $dept) 			{
				$dept->GenerNameOfPath();
				$this->GenerChildNameOfPath($dept->getNo());
				$emps = Emps::constructor__();
				$emps->Retrieve(EmpAttr::$FK_Dept, $this->getNo());
				foreach ($emps->ToJavaList() as $emp) 				{
					$emp->Update();
				}
			}
		}
	}
}
?>
